import requests
import os
from urllib.parse import urlencode
from pyquery import PyQuery as pq
import json


base_url = 'https://m.weibo.cn/api/container/getIndex?'
headers = {
	'Host':'m.weibo.cn',
    'Referer': 'https://m.weibo.cn/u/1402400261',
    'User-Agent':'Mozilla/s.o (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Sa ari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}


def get_url(sum_page):
    list_since_id = ['']
    sum_since_id = 0
    params = {
        'type':'uid',
        'value':'1402400261',
        'containerid':'1076031402400261',
        # 'since_id':'i'
    }
    url = base_url + urlencode(params)
    urls = [url]
    print(urls)
    for i in urls:
        if sum_since_id <= sum_page:
            # print(sum_since_id)
            try:
                res = requests.get(i, headers=headers)
                # print(res)
                if res.status_code == 200:
                    # list_since_id.append(res.json()['data']['cardlistInfo']['since_id']) 
                    since_id = res.json()['data']['cardlistInfo']['since_id']
                    url1 = url + '&since_id=' + str(since_id)
                    urls.append(url1)                   
                    # print(list_since_id)
                    # print(urls)
            except requests.ConnectionError as e:
                print('Error', e.args)
            sum_since_id += 1
        else:
            print('链接获取完成') 
    print(urls)
    return urls

def get_data(urls):
    for i in urls:
        res = requests.get(i, headers=headers).json()
        items = res.get('data').get('cards')
        # print(items)
        for item in items:
            item = item.get('mblog')
            weibo = {}
            weibo['id'] = item.get('id')
            weibo['内容'] = pq(item.get('text')).text()
            yield weibo

file_path = r'./wb_spider1_text1.txt'
file = open(file_path,'w', encoding='utf-8')


if __name__ == '__main__':
    urls = get_url(3)
    results = get_data(urls)
    for result in results:
        file.write(str(result) + "\n")